﻿using Finance.Web.Models;
using Finance.Web.Models.Accounting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace Finance.Web.Controllers
{
    public class ActiveController : Controller
    {
         [Authorize]
        public ActionResult Index()
        {
            return PartialView();
        }

         [Authorize]
        public ActionResult Deposites()
        {
            return PartialView();
        }


         [Authorize]
        public ActionResult NewUserDeposite()
        {
            return PartialView();
        }

         [Authorize]
        public ActionResult Query(string name, int offset, int count)
        {
            using (FinanceContext context = new FinanceContext())
            {

                FinanceMembershipUser user = (FinanceMembershipUser)Membership.GetUser();

                IEnumerable<UserDeposit> models = context.UserDeposites.Where(i => i.UserCode ==  user.Code)
                    .OrderBy(i => i.Deposit.Name);
                if (name != null && name != string.Empty)
                    models = models.Where(i => i.Deposit.Name.IndexOf(name, StringComparison.InvariantCultureIgnoreCase) != -1);

                models = models.Skip(offset).Take(count);

                return Json(models.Select(m => new UserDepositViewModel(m)).ToList());
            }
        }

        [Authorize]
        public ActionResult Get(int code)
        {
            using (FinanceContext context = new FinanceContext())
            {
                FinanceMembershipUser user = (FinanceMembershipUser)Membership.GetUser();

                var model = context.UserDeposites.FirstOrDefault(i => i.DepCode == code && i.UserCode == user.Code);
                if (model != null)
                    return Json(new UserDepositViewModel(model));
                else
                    return HttpNotFound();
            }
        }

        [Authorize]
        public ActionResult ShowResults(UserDeposit model)
        {
            DepositAmountAccounting depAmAc = new DepositAmountAccounting();
            double res = depAmAc.CalculateDeposit(model, DateTime.Now);
            return Json(res);
        }

        [Authorize]
        public ActionResult Remove(UserDeposit model)
        {
            using (FinanceContext context = new FinanceContext())
            {
                FinanceMembershipUser user = (FinanceMembershipUser)Membership.GetUser();

                var removeItems = context.UserDeposites.Where(i => i.UserCode == user.Code && i.DepCode == model.DepCode);

                if (removeItems.Count() != 0)
                {
                    context.UserDeposites.RemoveRange(removeItems);
                    context.SaveChanges();
                    return new JsonResult();
                }
                else
                    return HttpNotFound();
            }
        }

        [Authorize]
        public ActionResult Save(UserDeposit model)
        {
            if (ModelState.IsValid)
            {

                FinanceMembershipUser user = (FinanceMembershipUser)Membership.GetUser();

                using (FinanceContext context = new FinanceContext())
                {
                    var depCode = model.Deposit != null ? model.Deposit.Code : model.DepCode;

                    var exist = context.UserDeposites.FirstOrDefault(i => i.DepCode == depCode && i.UserCode == user.Code);

                    if (exist != null)
                    {
                        exist.OpeningDate = model.OpeningDate;
                        exist.EndDate = model.EndDate;
                        exist.Amount = model.Amount;

                        context.SaveChanges();
                        return new JsonResult();
                    }
                    else
                    {
                        model.Deposit = context.Deposites.FirstOrDefault(b => b.Code == depCode);
                        model.User = context.Users.FirstOrDefault(i => i.Code == user.Code);
                        context.UserDeposites.Add(model);

                        context.SaveChanges();
                        return new JsonResult();
                    }
                }
            }
            return Finance.Web.Utils.AngularFormErrors(this);
        }
    }
}
